require 'MVVM.Sources.View.Adaptor'
require 'MVVM.Sources.View.Adaptors.ListAdaptor'

ScrollSnapAdaptor = class('ScrollSnapAdaptor', Adaptor)

function ScrollSnapAdaptor:ctor(gameObject, propertyName, pagePrefab, pagiNationPrefab, binder)
    self.pagePrefab = pagePrefab
    self.pagiNationPrefab = pagiNationPrefab
    self.component = gameObject:GetComponent('HorizontalScrollSnap')
    self.pagiNationParent = self.component.Pagination.transform
    self:BindProperty(propertyName, binder)
    binder:AddEx(propertyName,
            function(viewModel) self:OnAdd(viewModel) end,
            function(index, viewModel) end,
            function(viewModel) self:OnRemove(viewModel) end)
end

function ScrollSnapAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if newValue == nil then
        return
    end

    local childCount = 0
    if self.component.ChildObjects ~= nil then
        childCount = self.component.ChildObjects.Length
    end
    local newCount = #newValue

    if childCount < newCount then
        for i = childCount + 1, newCount do
            local go = GameObject.Instantiate(self.pagePrefab)
            self.component:AddChild(go)
        end

        for i = childCount + 1, newCount do
            local go = GameObject.Instantiate(self.pagiNationPrefab)
            go.transform:SetParent(self.pagiNationParent, false)
            local paginationScript = go:GetComponent('PaginationScript')
            paginationScript.hss = self.component
            paginationScript.Page = i - 1
        end
    elseif childCount > newCount then
        for i = childCount - 1, newCount, -1 do
            local go = self.component:RemoveChild(i, nil)
            GameObject.Destroy(go)
        end

        for i = childCount - 1, newCount, -1 do
            local go = self.pagiNationParent:GetChild(i).gameObject
            go.transform:SetParent(nil)
            GameObject.Destroy(go)
        end
    end

    for i, viewModel in pairs(newValue) do
        local trans = self.component.ChildObjects[i - 1].transform
        local view = GetLuaObject(trans)
        view:Set_BindingContext(viewModel)

        UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(trans)
    end

    UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.component.transform)
end

function ScrollSnapAdaptor:OnAdd(viewModel)
    local go = GameObject.Instantiate(self.pagePrefab)
    self.component:AddChild(go)

    local paginationGo = GameObject.Instantiate(self.pagiNationPrefab)
    paginationGo.transform:SetParent(self.pagiNationParent, false)
    local paginationScript = paginationGo:GetComponent('PaginationScript')
    paginationScript.hss = self.component
    paginationScript.Page = self.component.ChildObjects.Length - 1

    UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(go.transform)
    local view = GetLuaObject(go)
    if view then
        view:Set_BindingContext(viewModel)
    end

    UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(self.component.transform)
end

function ScrollSnapAdaptor:OnRemove(index)
    local go = self.component:RemoveChild(index, nil)
    GameObject.Destroy(go)
end